{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "4a76087e-4546-4cc4-96b4-710dc6f1f05c",
   "metadata": {},
   "source": [
    "# Correlation Heatmap\n",
    "\n",
    "A heatmap showing pairwise correlation between numerical features in the penguins dataset. Darker colors represent stronger positive or negative correlations."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "613bdf40-dd10-443e-b082-2c0a91fb9af0",
   "metadata": {},
   "outputs": [],
   "source": [
    "import hvplot.pandas  # noqa\n",
    "\n",
    "df = hvplot.sampledata.penguins(\"pandas\")\n",
    "# Compute correlation matrix\n",
    "corr = df[[c for c in df.columns if c.split(\"_\")[-1] in (\"mm\", \"g\")]].corr()\n",
    "# Convert to long-form for heatmap\n",
    "corr_df = corr.stack().reset_index()\n",
    "corr_df.columns = ['variable_1', 'variable_2', 'correlation']\n",
    "\n",
    "corr_df.hvplot.heatmap(\n",
    "    x='variable_1',\n",
    "    y='variable_2',\n",
    "    C='correlation',\n",
    "    cmap='coolwarm',\n",
    "    clim=(-1, 1),\n",
    "    title='Correlation Heatmap (Bokeh)',\n",
    "    hover_tooltips=[\n",
    "        (\"Variable 1\", \"@variable_1\"),\n",
    "        (\"Variable 2\", \"@variable_2\"),\n",
    "        (\"Correlation\", \"@correlation\")\n",
    "    ]\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bf073f35-ceb8-4515-b864-9c312790235d",
   "metadata": {},
   "outputs": [],
   "source": [
    "import hvplot.pandas  # noqa\n",
    "hvplot.extension('matplotlib')\n",
    "\n",
    "df = hvplot.sampledata.penguins(\"pandas\")\n",
    "# Compute correlation matrix\n",
    "corr = df[[c for c in df.columns if c.split(\"_\")[-1] in (\"mm\", \"g\")]].corr()\n",
    "# Convert to long-form for heatmap\n",
    "corr_df = corr.stack().reset_index()\n",
    "corr_df.columns = ['variable_1', 'variable_2', 'correlation']\n",
    "\n",
    "corr_df.hvplot.heatmap(\n",
    "    x='variable_1',\n",
    "    y='variable_2',\n",
    "    C='correlation',\n",
    "    cmap='coolwarm',\n",
    "    clim=(-1, 1),\n",
    "    title='Correlation Heatmap (Matplotlib)',\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c45fd2be-dec8-459d-bfe5-4154e24f14a8",
   "metadata": {},
   "outputs": [],
   "source": [
    "corr_df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c66ae024-4ec7-4b32-8b15-2efa7f01531c",
   "metadata": {},
   "source": [
    ":::{seealso}\n",
    "- [Heatmap reference documentation](../../ref/api/manual/hvplot.hvPlot.heatmap.ipynb).\n",
    ":::"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
